"use client";

import { auth, signOut } from "@/auth";
import { Button } from "@/components/ui/button";
import { useEffect, useState } from "react";
import { Session } from "next-auth";

export default function Home() {
  const [session, setSession] = useState<Session | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    auth().then((session) => {
      setSession(session);
      setLoading(false);
    });
  }, []);

  if (loading) {
    return <div className="flex justify-center p-8">Loading...</div>;
  }

  if (!session?.user) {
    return <div className="flex justify-center p-8">Not authenticated</div>;
  }

  return (
    <div className="flex flex-col items-center justify-center min-h-screen gap-4">
      <h1 className="text-2xl font-bold">Welcome, {session.user.name}!</h1>
      <p>Email: {session.user.email}</p>
      <Button
        onClick={async () => {
          await signOut();
          window.location.href = "/login";
        }}
      >
        Sign Out
      </Button>
    </div>
  );
}
